﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QLBH.SaleProduct.Presenter;
using QLBH.FeatureBase;
using QLBH.Base;
using QLBH.DataModel;
using QLBH.Model.Controller;
using System.Windows.Forms;
using System.Data;
using QLBH.StandardUI;
using QLBH.ModelInterface;

namespace QLBH.SaleProduct
{
    public class DSDonMuaHangView:BaseView
    {
        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // DSDonMuaHangView
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
            this.Name = "DSDonMuaHangView";
            this.Size = new System.Drawing.Size(1502, 872);
            this.ResumeLayout(false);

        }        
        public override void LoadForm(object sender, EventArgs e)
        {
            base.LoadForm(sender, e);
            RefreshData(sender, e);
        }
        public override void RefreshData(object sender, System.EventArgs e)
        {
            base.RefreshData(sender, e);

            LoadDSPhieuMuaHang();

            DataGridView.Columns["SOPHIEU"].HeaderText = "Số phiếu";
            DataGridView.Columns["SOPHIEU"].DisplayIndex = 0;            

            DataGridView.Columns["MaPhieu"].Visible = false;
            DataGridView.Columns["MaPhieu"].DisplayIndex = 8;

            DataGridView.Columns["NgayTao"].HeaderText = "Ngày tạo";
            DataGridView.Columns["NgayTao"].DisplayIndex = 2;            

            DataGridView.Columns["TongTien"].HeaderText = "Tổng tiền";
            DataGridView.Columns["TongTien"].DisplayIndex = 1;

            DataGridView.Columns["UserID"].Visible = false;
            DataGridView.Columns["UserID"].DisplayIndex = 7;

            DataGridView.Columns["UserName"].HeaderText = "Người tạo";
            DataGridView.Columns["UserName"].DisplayIndex = 4;

            DataGridView.Columns["TenKho"].HeaderText = "Tên kho";
            DataGridView.Columns["TenKho"].DisplayIndex = 3;

            DataGridView.Columns["MaKho"].Visible = false;
            DataGridView.Columns["MaKho"].DisplayIndex = 5;

            DataGridView.Columns["NgayGui"].Visible = false;//display when support send Phieu Mua hang via email
            DataGridView.Columns["NgayGui"].DisplayIndex = 6;
        }

        private void LoadDSPhieuMuaHang()
        {
            QLPhieuMuaHangPresenter presenter = new QLPhieuMuaHangPresenter();
            DataSource=presenter.GetAllPhieuMuaHang();

            DataGridView.DataSource = DataSource;            

            //Change data grid view header           
              
        }
        public override void AddNew(object sender, EventArgs e)
        {
            base.AddNew(sender, e);

            PhieuMuaHangDlg addNew = new PhieuMuaHangDlg();
            addNew.DlgType = eDlgType.ADD;

            IPhieuMuaHangChiTietModel mhChiTiet = ModelFactory.ModelFactory.GetPhieuMuaHangChiTietModelInstance();
            mhChiTiet.NgayTao = DateTime.Now;
            mhChiTiet.SoPhieu = CodeHelper.GetNewCode();
            KHO defautKho = KhoController.GetInstance().GetKhoDefault();
            if (defautKho!=null)
            {
                mhChiTiet.TenKho = defautKho.TENKHO;
                mhChiTiet.MaKho = defautKho.KHOID;
            }
            mhChiTiet.TongTien = 0;
            mhChiTiet.UserID = AccountHandler.GetCurrentUserLogin().NVID;
            mhChiTiet.UserName = AccountHandler.GetCurrentUserLogin().TENNV;

            addNew.Model = mhChiTiet;
            
            
            if (addNew.ShowDialog()==DialogResult.OK)
            {
                RefreshData(sender, null); 
            }   
        }
        public override void EditItem(object sender, EventArgs e)
        {
            base.EditItem(sender, e);

            PhieuMuaHangDlg addNew = new PhieuMuaHangDlg();
            addNew.DlgType = eDlgType.EDIT;

            IPhieuMuaHangModel model = base.DataGridView.SelectedRows[0].DataBoundItem as IPhieuMuaHangModel;

            IPhieuMuaHangChiTietModel mhChiTiet = ModelFactory.ModelFactory.GetPhieuMuaHangChiTietModelInstance();
            
            addNew.Model = mhChiTiet;


            if (addNew.ShowDialog() == DialogResult.OK)
            {
                RefreshData(sender, null);
            }   
        }
        public override void DeleteItem(object sender, EventArgs e)
        {
            if (ShowMessage.Instance.ShowQuestion("Bạn muốn xóa phiếu mua hàng này?")==DialogResult.Yes)
            {
                base.DeleteItem(sender, e);

                IPhieuMuaHangModel model = base.DataGridView.SelectedRows[0].DataBoundItem as IPhieuMuaHangModel;
                PhieuMuaHangController.GetInstance().Delete(model.ToEntity());
                LoadDSPhieuMuaHang();
            }                                    
            
        }
    }
}
